package com.agile.leetcode.jsonpath.readjson;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import lombok.Data;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author chenzhangkun
 * @ClassName ReadJsonDemo
 * @description: TODO
 * @date 2025年04月27日
 */
public class ReadJsonDemo {
    public static void main(String[] args) {
        String path="E:\\迅雷云盘\\全国JSON\\Citys3465个\\ChinaCitys.json";
        // 加载文件数据转为json
        JSON json = JSONUtil.readJSON(new File(path), Charset.defaultCharset());
        List<Province> provinceList = json.toBean(new TypeReference<>() {
        });
        List<Region> region = RegionUtil.getRegion();
        Map<String, Region> codeRegionMap = region.stream().collect(Collectors.toMap(Region::getId, Function.identity(), (oldValue, newValue) -> oldValue));
        Map<String, Region> snameRegionMap = region.stream().collect(Collectors.toMap(Region::getSname, Function.identity(), (oldValue, newValue) -> oldValue));
        List<String> list=new ArrayList<>();
        for (Province province : provinceList) {
            if (StrUtil.equals(province.getProvince(),"台湾省")){
                continue;
            }
            Region provinceRegion = codeRegionMap.get(province.getCode());
            System.out.println("insert into region_test(id,name,pid,sname,level,mername,Lng,Lat,pinyin) values('"+province.getCode()+"','"+province.getProvince()+"',"+100000+",'"+province.getProvince()+"',"+1+",'"+provinceRegion.getMername()+"',"+provinceRegion.getLng()+","+provinceRegion.getLat()+",'"+provinceRegion.getPinyin().replaceAll("'","")+"');");
            List<City> citys = province.getCitys();
            for (City city : citys) {
                Region cityRegion = codeRegionMap.get(city.getCode());
                if (cityRegion != null){
                    System.out.println("insert into region_test(id,name,pid,sname,level,citycode,yzcode,mername,Lng,Lat,pinyin) values('"+city.getCode()+"','"+city.getCity()+"','"+cityRegion.getPid()+"','"+city.getCity()+"',"+2+",'"+cityRegion.getCitycode()+"','"+cityRegion.getYzcode()+"','"+cityRegion.getMername()+"',"+cityRegion.getLng()+","+cityRegion.getLat()+",'"+cityRegion.getPinyin().replaceAll("'","")+"');");
                }else {
                    System.out.println("insert into region_test(id,name,pid,sname,level) values('"+city.getCode()+"','"+city.getCity()+"','"+province.getCode()+"','"+city.getCity()+"',2);");
                }
                List<Area> areas = city.getAreas();
                for (int i = 0; i < areas.size(); i++) {
                    Area area = areas.get(i);
                    if (StrUtil.equals(city.getCity(),"东莞市")||StrUtil.equals(city.getCity(),"中山市")||StrUtil.equals(city.getCity(),"儋州市")){
                        area.setCode(Integer.parseInt(area.getCode())+(i+1)+"");
                    }
                    Region areaRegion;
                    Region areaRegion1 = codeRegionMap.get(area.getCode());
                    if (areaRegion1!=null){
                        areaRegion=areaRegion1;
                    }else {
                        areaRegion = snameRegionMap.get(area.getArea().substring(0,area.getArea().length()-1));
                    }
                    if (areaRegion != null){
                        System.out.println("insert into region_test(id,name,pid,sname,level,citycode,yzcode,mername,Lng,Lat,pinyin) values('"+area.getCode()+"','"+area.getArea()+"','"+city.getCode()+"','"+area.getArea().substring(0,area.getArea().length()-1)+"',"+3+",'"+areaRegion.getCitycode()+"','"+areaRegion.getYzcode()+"','"+areaRegion.getMername()+"',"+areaRegion.getLng()+","+areaRegion.getLat()+",'"+areaRegion.getPinyin().replaceAll("'","")+"');");

                    }else {
                        System.out.println("insert into region_test(id,name,pid,sname,level) values('"+area.getCode()+"','"+area.getArea()+"','"+city.getCode()+"','"+area.getArea().substring(0,area.getArea().length()-1)+"',3);");
                    }
                }

            }
        }
        System.out.println(list);
        /*for (Province province : provinceList) {
            List<City> citys = province.getCitys();
            for (City city : citys) {
                Region cityRegion = codeRegionMap.get(city.getCode());
                if (cityRegion != null){
                    if (!StrUtil.equals(cityRegion.getName(),city.getCity())){
                        System.out.println("update from region set name ="+city.getCity()+" where id ="+cityRegion.getId());
                    }
                }else {
                    // 名称有没有符合的
                    Region nameCityRegion = snameRegionMap.get(city.getCity().substring(0,city.getCity().length()-1));
                    if (nameCityRegion!=null){
                        System.out.println("update from region set name ="+city.getCity()+" where id ="+nameCityRegion.getId());
                    }else {
                        System.out.println("insert into region values("+city.getCode()+",'"+city.getCity()+"','"+province.getCode()+"','"+city.getCity().substring(0,city.getCity().length()-1)+"',2,null,null,null,null,null,null)");
                    }
                }
                List<Area> areas = city.getAreas();
                for (Area area : areas) {
                    Region areaRegion = codeRegionMap.get(area.getCode());
                    if (areaRegion != null){
                        if (!StrUtil.equals(areaRegion.getName(),area.getArea())){
                            System.out.println("update from region set name ="+city.getCity()+"where id ="+areaRegion.getId());
                        }
                    }else {
                        // 名称有没有符合的
                        Region nameCityRegion = snameRegionMap.get(area.getArea().substring(0,area.getArea().length()-1));
                        if (nameCityRegion!=null){
                            System.out.println("update from region set name ="+area.getArea()+" where id ="+nameCityRegion.getId());
                        }else {
                            System.out.println("insert into region values("+area.getCode()+",'"+area.getArea()+"','"+city.getCode()+"','"+area.getArea().substring(0,area.getArea().length()-1)+"',3,null,null,null,null,null,null)");
                        }
                    }
                }

            }
        }*/
    }
    @Data
    public static class Province{
        private String province;
        private String code;
        private List<City> citys;

        public void setCode(String code) {
            this.code=code.substring(0,6);
        }
    }
    @Data
    public static class City {
        private String city;
        private String code;
        private List<Area> areas;
        public void setCode(String code) {
            this.code=code.substring(0,6);
        }
    }
    @Data
    public static class Area {
        private String area;
        private String code;
        public void setCode(String code) {
            this.code=code.substring(0,6);
        }
    }
}
